#include <iostream>
#include <cassert>
#include <cstring>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

using namespace std;

int main()
{
    int n = mkfifo("workFifo", 0666);
    if(n < 0)
    {
        cerr << "创建管道文件失败" << endl;
        return 1;
    }
    char buffer[1024];
    // 打开文件
    int rfd = open("workFifo", O_RDONLY);
    if(rfd < 0)
    {
        cerr << "打开管道文件失败" << endl;
        return 1;
    }
    cout << "可以准备从管道中读数据了" << endl;
    while(true)
    {
        // 一直读取
        int num = read(rfd, buffer, 100);
        if(num > 0)
        {
            cout << "本次读取到了" << num << "个字符" << endl;
            buffer[num] = 0;
            cout << "读取到了: " << buffer << endl;
        }
        else if(num == 0)
        {
            cout << "读取失败，没有进程向管道中写文件了" << endl;
            break;
        }
    }
    close(rfd);
    return 0;
}